#ifndef __SDP_PARSER_H_FILE_k2398429k__
#define __SDP_PARSER_H_FILE_k2398429k__


#ifdef WIN32
#pragma warning(disable:4786)
#endif

#include <string>
#include <list>
#include <vector>
#include "OSHeaders.h"
#include "OSMutex.h"

using namespace std;

class CSdpLineItem;
class CSdpMediaAnnounceItem;

// added by samsmith
bool ParserConnectData(const char* item,char *v_ip,unsigned short *v_ttl);
void ModifySdpConnectIp(CSdpLineItem*,const string&);
// end;

typedef list<CSdpLineItem*>  ITEM_LIST;
class CSdpParser
{
public:
	~CSdpParser();
	Bool SetAndParserFromFile(const string& strFileName);
	void SetAndParser(const string& strSdp);
	
	UInt32 GetTrackNum() { return m_vecMediaAnnounces.size(); }
	string GetSdp();
	void ModifyTrackID(); //modify TrackID, Video=1; Audio=2;
	void RemoveALine(const string strKey, const string strAttribute="");
	
	Float32 GetRange(string& outTimeType,Float32& vStart,Float32& vEnd);
	Float32 GetRange(string& outTimeType);

	CSdpMediaAnnounceItem* GetMediaAnnounceItem(const string& strMediaType);
	CSdpMediaAnnounceItem* GetMediaAnnounceItem(UInt32 nIndex);

	// for support rm
	void  ProduceRMSdp();
	void  RemoveRMSdpRange();
	void  ModifyRMSdpLenth(int len);
	
	// added by samsmith
	string GetGeneralDestIP();
	void Reset();
	Bool ModifyConnectIp(const string& ip);
	Bool AddGeneralControl();
	Bool AddALine(const string line);
	// end;
private:

private:
	OSMutex			m_mtxSdpParser;
	ITEM_LIST			m_vecGeneralLines;
	vector<CSdpMediaAnnounceItem*>		m_vecMediaAnnounces;
};

class CSdpMediaAnnounceItem
{
public:
	~CSdpMediaAnnounceItem();
	CSdpMediaAnnounceItem(){m_uiPayload=0xFFFFFFFF;m_bSamePayload=FALSE;m_lsMixedTrackID.clear();}
	
	void AddAline(const string& strLine);
	void AddALine(CSdpLineItem* pLineItem);
	void ModifyALine(const string& strLine);
	
	string GetMediaAnnouce();

	UInt32 GetTrackID();
	string GetMediaType();
	UInt32 GetTimeScale();
	UInt16 GetDestPort();
	string GetDestIP();
	string GetCodec();
	// added by samsmith
	void RemoveALine(const string strKey, const string strAttribute="");
	Bool GetRange(string& outTimeType,Float32& vStart,Float32& vEnd);
	string GetDestIP(unsigned short *v_ttl);
	Bool ModifyConnectIp(const string& nip);
	Bool ModifyConnectPort(UInt16 nport=0);
	UInt32 GetPayload(){return m_uiPayload;};
	std::list<UInt32>* GetMixedTrackID(){return &m_lsMixedTrackID;};
	Bool   HaveSamePayload(){return m_bSamePayload;};
	void   SetSamePayload(Bool bSame,UInt32 trackid)
		{
			Bool bExisted = FALSE;
			std::list<UInt32>::iterator it = m_lsMixedTrackID.begin();			
			for( ;it!= m_lsMixedTrackID.end();it++)
			{
				if( *it == trackid )
				{
					bExisted = TRUE;
					break;
				}
			}
			if( !bExisted )
			{
				m_bSamePayload = bSame;
				m_lsMixedTrackID.push_back(trackid);
			}
		};
	// end;

private:
	vector<CSdpLineItem*>	m_vecLines;

	UInt32                  m_uiPayload;
	Bool                    m_bSamePayload;
	std::list<UInt32>       m_lsMixedTrackID;// show which trackid have the same payload
};


class CSdpLineItem
{
public:
	void SetAndParserALine(const string& strALine);
	string& GetAndSetKey(const string strKey="");
	string& GetAndSetAttribute(const string strAttr="");
	string& GetAndSetValue(const string strVal="");
	
	string GetThisLine();

	void SetHasAttribute(Bool bHave){ m_hasAttribute = bHave;};
	Bool HasAttribute() { return m_hasAttribute; }

	Bool operator == (const CSdpLineItem& other);
private:
	std::string		m_strKey;			//
	Bool			m_hasAttribute;		//
	std::string		m_strAttribute;		//
	std::string		m_strValue;			//
	
};


#endif

